package com.netease.xone.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;

public class CommonListView extends ListView implements OnScrollListener{
	private static final String TAG = "CommonListView";
	 private Context mContext;
	 private OnScrollListener mOnScrollListener;
	 private OnScrollToBottomListener mScrollToBottomListener;
	 private int mCurrentScrollState;
	 private View mFootView;
	 private ProgressBar mFootProgress;
	 private TextView mFootText;
	 
	 public static final int FOOT_STATE_LODING = 0;
	 public static final int FOOT_STATE_GONE = 1;
	 public static final int FOOT_STATE_NO_DATA = 2;

	 private int mFootState = FOOT_STATE_GONE;
	 

	public CommonListView(Context context) {
		super(context);
		mContext = context;
		super.setOnScrollListener(this);
	}
	public CommonListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		super.setOnScrollListener(this);
	}
	public CommonListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		super.setOnScrollListener(this);
	}
	
    @Override
    public void setOnScrollListener(AbsListView.OnScrollListener l) {
        mOnScrollListener = l;
    }

    public void setOnScrollToBottomListener(OnScrollToBottomListener l){
    	mScrollToBottomListener = l;
    }

    public void addCommonFooterView(){
    	mFootView = LayoutInflater.from(mContext).inflate(R.layout.foot_loading, null);
		mFootProgress = (ProgressBar) mFootView.findViewById(R.id.foot_progress);
		mFootText = (TextView) mFootView.findViewById(R.id.foot_text);
		addFooterView(mFootView);
		setFootState(mFootState);
    }
    
    public void setFootState(int state){  
    	 NTLog.i(TAG, "setFootState state is " + state);
    	 if(mFootView != null && getFooterViewsCount() > 0){
	    	switch(state){
	    	 case FOOT_STATE_LODING:
		    		 mFootView.setVisibility(View.VISIBLE);
		    		 setFooterDividersEnabled(true);
		    		 mFootProgress.setVisibility(View.VISIBLE);
		    		 mFootText.setText(mContext.getString(R.string.foot_querying));
	    		 break;
	    	 case FOOT_STATE_GONE:
		    		setFooterDividersEnabled(false);
		 			mFootView.setVisibility(View.GONE);
	 			break;
	    	 case FOOT_STATE_NO_DATA:
		    		 mFootView.setVisibility(View.GONE);
		    		 removeFooterView(mFootView);
	    		 break;
	 		default:
	 			return;
	    	}
	    	mFootState = state;
    	}
    }

    public int getFootState(){
    	return mFootState;
    }
    
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (mOnScrollListener != null) {
			mOnScrollListener.onScroll(view, firstVisibleItem,
					visibleItemCount, totalItemCount);
		}
//		NTLog.i(TAG, "onScroll mCurrentScrollState is " + mCurrentScrollState + " mScrollToBottomListener is " + mScrollToBottomListener);
		if(mCurrentScrollState != SCROLL_STATE_IDLE && mScrollToBottomListener != null){
			int last = firstVisibleItem + visibleItemCount;
//			NTLog.i(TAG, "onScroll last is " + last + " totalItemCount is " + totalItemCount);
			if(last == totalItemCount && mFootState != FOOT_STATE_LODING ){
				mScrollToBottomListener.OnScrollToBottom();
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		mCurrentScrollState = scrollState;

		if (mOnScrollListener != null) {
			mOnScrollListener.onScrollStateChanged(view, scrollState);
		}
	}
	
	 public interface OnScrollToBottomListener{
	    	public void OnScrollToBottom();
	 }
}
